package com.sdonkey.score.service.impl;

import com.sdonkey.score.bean.UserLoginBean;
import com.sdonkey.score.dao.IUserLoginDao;
import com.sdonkey.score.dto.UserInfoDto;
import com.sdonkey.score.service.IUserLoginService;
import com.sdonkey.score.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户登录服务实现类
 *
 * @author ZhaoShihao
 * @version 1.0
 */
@Service
@Transactional(propagation= Propagation.REQUIRED,readOnly = true)
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    IUserLoginDao userLoginDao;

    @Override
    public UserLoginBean userLogin(String username, String login_password) {
        UserLoginBean bean = userLoginDao.userLogin(username, MD5.encode(login_password));
        /*if (bean != null) {
            userLoginDao.updateLoginStatus(bean.getDevId());
        }*/
        return bean;
    }

    @Override
    public Integer hasEmail(String email) {
        return userLoginDao.hasEmail(email);
    }

    @Override
    public Integer hasPhone(String phone) {
        return userLoginDao.hasPhone(phone);
    }

    @Override
    @Transactional
    public void resetPassword(String password, Integer id) {
        userLoginDao.resetPassword(MD5.encode(password), id);
        // TODO: 2016/11/8 0008  用户更改密码成功之后要发送通知给用户
    }

    @Override
    public UserInfoDto getUser(Integer id) {
        UserLoginBean user = null;
        if (id != null) {
            user = userLoginDao.getUser(id);
        }
        return new UserInfoDto(user);
    }
}
